<template>
  <v-dialog
    :title="title"
    :close-on-click-modal="false"
    v-model="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="项目id" prop="project.id"
                :rules="[
                 ]">
	       <GridSelect
            title="选择项目id"
            labelName = 'projectName'
            labelValue = 'id'
            v-model = "inputForm.project.id"
            :limit="1"
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'projectName',
              label: '项目名称'
            }
            ]"
            :searchs="[
            {
              prop: 'projectName',
              label: '项目名称'
            }
            ]"
            :api="projectMainService">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="合同金额" prop="contractAmt"
                :rules="[
                 ]">
              <el-input v-model="inputForm.contractAmt" placeholder="请填写合同金额"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="合同审批金额" prop="auditContractAmt"
                :rules="[
                 ]">
              <el-input v-model="inputForm.auditContractAmt" placeholder="请填写合同审批金额"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="不可预见费" prop="unForeseenExpenses"
                :rules="[
                 ]">
              <el-input v-model="inputForm.unForeseenExpenses" placeholder="请填写不可预见费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="不可预见审批费" prop="auditUnForeseenExpenses"
                :rules="[
                 ]">
              <el-input v-model="inputForm.auditUnForeseenExpenses" placeholder="请填写不可预见审批费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="中标服务费" prop="winningServiceFee"
                :rules="[
                 ]">
              <el-input v-model="inputForm.winningServiceFee" placeholder="请填写中标服务费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="中标服务审批费" prop="auditWinningServiceFee"
                :rules="[
                 ]">
              <el-input v-model="inputForm.auditWinningServiceFee" placeholder="请填写中标服务审批费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="标书制作费" prop="biddingDocumentFee"
                :rules="[
                 ]">
              <el-input v-model="inputForm.biddingDocumentFee" placeholder="请填写标书制作费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="标书制作审批费" prop="auditBiddingDocumentFee"
                :rules="[
                 ]">
              <el-input v-model="inputForm.auditBiddingDocumentFee" placeholder="请填写标书制作审批费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="设备费" prop="equipmentCost"
                :rules="[
                 ]">
              <el-input v-model="inputForm.equipmentCost" placeholder="请填写设备费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="设备审批费" prop="auditEquipmentCost"
                :rules="[
                 ]">
              <el-input v-model="inputForm.auditEquipmentCost" placeholder="请填写设备审批费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="施工费" prop="constructionCost"
                :rules="[
                 ]">
              <el-input v-model="inputForm.constructionCost" placeholder="请填写施工费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="施工审批费" prop="auditConstructionCost"
                :rules="[
                 ]">
              <el-input v-model="inputForm.auditConstructionCost" placeholder="请填写施工审批费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="工程保险费" prop="engineeringInsurance"
                :rules="[
                 ]">
              <el-input v-model="inputForm.engineeringInsurance" placeholder="请填写工程保险费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="工程保险审批费" prop="auditEngineeringInsurance"
                :rules="[
                 ]">
              <el-input v-model="inputForm.auditEngineeringInsurance" placeholder="请填写工程保险审批费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="安全生产费" prop="safetyProductionCost"
                :rules="[
                 ]">
              <el-input v-model="inputForm.safetyProductionCost" placeholder="请填写安全生产费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="安全生产审批费" prop="auditSafetyProductionCost"
                :rules="[
                 ]">
              <el-input v-model="inputForm.auditSafetyProductionCost" placeholder="请填写安全生产审批费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="科研费" prop="researchFunding"
                :rules="[
                 ]">
              <el-input v-model="inputForm.researchFunding" placeholder="请填写科研费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="科研审批费" prop="auditResearchFunding"
                :rules="[
                 ]">
              <el-input v-model="inputForm.auditResearchFunding" placeholder="请填写科研审批费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="人员管理费" prop="personnelManagementFee"
                :rules="[
                 ]">
              <el-input v-model="inputForm.personnelManagementFee" placeholder="请填写人员管理费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="人员管理审批费" prop="auditPersonnelManagementFee"
                :rules="[
                 ]">
              <el-input v-model="inputForm.auditPersonnelManagementFee" placeholder="请填写人员管理审批费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="其他管理费" prop="otherManagementFee"
                :rules="[
                 ]">
              <el-input v-model="inputForm.otherManagementFee" placeholder="请填写其他管理费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="其他管理审批费" prop="auditOtherManagementFee"
                :rules="[
                 ]">
              <el-input v-model="inputForm.auditOtherManagementFee" placeholder="请填写其他管理审批费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="其他" prop="miscellaneous"
                :rules="[
                 ]">
              <el-input v-model="inputForm.miscellaneous" placeholder="请填写其他"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="其他审批费" prop="auditMiscellaneous"
                :rules="[
                 ]">
              <el-input v-model="inputForm.auditMiscellaneous" placeholder="请填写其他审批费"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="税金" prop="taxes"
                :rules="[
                 ]">
              <el-input v-model="inputForm.taxes" placeholder="请填写税金"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="税金审批费" prop="auditTaxes"
                :rules="[
                 ]">
              <el-input v-model="inputForm.auditTaxes" placeholder="请填写税金审批费"     ></el-input>
           </el-form-item>
        </el-col>
    <el-col :span="24">
      
       </el-col>
     </el-row>
    </el-form>
    <template #footer>    
        <span class="dialog-footer">
          <el-button @click="visible = false" icon="circle-close">关闭</el-button>
          <el-button  type="primary" v-if="method != 'view'" @click="doSubmit()" icon="circle-check" v-noMoreClick>确定</el-button>
        </span>
    </template>    
  </v-dialog>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  import projectCostService from '@/api/cost/projectCostService'
  import projectMainService from '@/api/project/projectMainService'   

  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        projectCostTab: '0',
        projectMainService: projectMainService,  
        inputForm: {
          id: '',
          costEquipmentDTOList: [],
          costConstructionDTOList: [],
          costSafetyDTOList: [],
          costResearchDTOList: [],
          costManagementDTOList: [],
          costOtherDTOList: [],
          project: {
            id: ''
          },
          contractAmt: '',
          auditContractAmt: '',
          unForeseenExpenses: '',
          auditUnForeseenExpenses: '',
          winningServiceFee: '',
          auditWinningServiceFee: '',
          biddingDocumentFee: '',
          auditBiddingDocumentFee: '',
          equipmentCost: '',
          auditEquipmentCost: '',
          constructionCost: '',
          auditConstructionCost: '',
          engineeringInsurance: '',
          auditEngineeringInsurance: '',
          safetyProductionCost: '',
          auditSafetyProductionCost: '',
          researchFunding: '',
          auditResearchFunding: '',
          personnelManagementFee: '',
          auditPersonnelManagementFee: '',
          otherManagementFee: '',
          auditOtherManagementFee: '',
          miscellaneous: '',
          auditMiscellaneous: '',
          taxes: '',
          auditTaxes: ''
        }
      }
    },
    components: {
      GridSelect
    },
    created () {
    },
    methods: {
      init (method, id) {
        this.method = method
        if (method === 'add') {
          this.title = `新建成本`
        } else if (method === 'edit') {
          this.title = '修改成本'
        } else if (method === 'view') {
          this.title = '查看成本'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.id = id
          this.projectCostTab = '0'
          this.inputForm.costEquipmentDTOList = []
          this.inputForm.costConstructionDTOList = []
          this.inputForm.costSafetyDTOList = []
          this.inputForm.costResearchDTOList = []
          this.inputForm.costManagementDTOList = []
          this.inputForm.costOtherDTOList = []
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            projectCostService.queryById(this.inputForm.id).then((data) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.loading = false
            })
          }
        })
      },
      saveCostEquipmentRow (child) {
        if (child[0] === '') {
          this.inputForm.costEquipmentDTOList.push(child[1])
        } else {
          this.inputForm.costEquipmentDTOList.forEach((item, index) => {
            if (item === child[0]) {
              this.inputForm.costEquipmentDTOList.splice(index, 1, child[1])
            }
          })
        }
      },
      addCostEquipmentRow () {
        this.inputForm.costEquipmentDTOList.push({
          listNo: '',
          listName: '',
          brandModel: '',
          unit: '',
          quantity: '',
          listUnitPrice: '',
          listTotalPrice: '',
          budgetUnitPrice: '',
          budgetTotalPrice: '',
          auditUnitPrice: '',
          auditTotalPrice: '',
          remark: ''
        })
      },
      delCostEquipmentRow (child) {
        this.inputForm.costEquipmentDTOList.forEach((item, index) => {
          if (item === child && item.id === '') {
            this.inputForm.costEquipmentDTOList.splice(index, 1)
          } else if (item === child) {
            item.delFlag = '1'
            this.inputForm.costEquipmentDTOList.splice(index, 1, item)
          }
        })
      },
      saveCostConstructionRow (child) {
        if (child[0] === '') {
          this.inputForm.costConstructionDTOList.push(child[1])
        } else {
          this.inputForm.costConstructionDTOList.forEach((item, index) => {
            if (item === child[0]) {
              this.inputForm.costConstructionDTOList.splice(index, 1, child[1])
            }
          })
        }
      },
      addCostConstructionRow () {
        this.inputForm.costConstructionDTOList.push({
          listNo: '',
          listName: '',
          unit: '',
          quantity: '',
          listUnitPrice: '',
          listTotalPrice: '',
          budgetUnitPrice: '',
          budgetTotalPrice: '',
          auditUnitPrice: '',
          auditTotalPrice: '',
          remark: ''
        })
      },
      delCostConstructionRow (child) {
        this.inputForm.costConstructionDTOList.forEach((item, index) => {
          if (item === child && item.id === '') {
            this.inputForm.costConstructionDTOList.splice(index, 1)
          } else if (item === child) {
            item.delFlag = '1'
            this.inputForm.costConstructionDTOList.splice(index, 1, item)
          }
        })
      },
      saveCostSafetyRow (child) {
        if (child[0] === '') {
          this.inputForm.costSafetyDTOList.push(child[1])
        } else {
          this.inputForm.costSafetyDTOList.forEach((item, index) => {
            if (item === child[0]) {
              this.inputForm.costSafetyDTOList.splice(index, 1, child[1])
            }
          })
        }
      },
      addCostSafetyRow () {
        this.inputForm.costSafetyDTOList.push({
          listNo: '',
          projectName: '',
          unit: '',
          quantity: '',
          budgetUnitPrice: '',
          budgetTotalPrice: '',
          auditUnitPrice: '',
          auditTotalPrice: '',
          remark: ''
        })
      },
      delCostSafetyRow (child) {
        this.inputForm.costSafetyDTOList.forEach((item, index) => {
          if (item === child && item.id === '') {
            this.inputForm.costSafetyDTOList.splice(index, 1)
          } else if (item === child) {
            item.delFlag = '1'
            this.inputForm.costSafetyDTOList.splice(index, 1, item)
          }
        })
      },
      saveCostResearchRow (child) {
        if (child[0] === '') {
          this.inputForm.costResearchDTOList.push(child[1])
        } else {
          this.inputForm.costResearchDTOList.forEach((item, index) => {
            if (item === child[0]) {
              this.inputForm.costResearchDTOList.splice(index, 1, child[1])
            }
          })
        }
      },
      addCostResearchRow () {
        this.inputForm.costResearchDTOList.push({
          listNo: '',
          categoryName: '',
          listName: '',
          brandModel: '',
          unit: '',
          quantity: '',
          listUnitPrice: '',
          listTotalPrice: '',
          budgetUnitPrice: '',
          budgetTotalPrice: '',
          auditUnitPrice: '',
          auditTotalPrice: '',
          remark: ''
        })
      },
      delCostResearchRow (child) {
        this.inputForm.costResearchDTOList.forEach((item, index) => {
          if (item === child && item.id === '') {
            this.inputForm.costResearchDTOList.splice(index, 1)
          } else if (item === child) {
            item.delFlag = '1'
            this.inputForm.costResearchDTOList.splice(index, 1, item)
          }
        })
      },
      saveCostManagementRow (child) {
        if (child[0] === '') {
          this.inputForm.costManagementDTOList.push(child[1])
        } else {
          this.inputForm.costManagementDTOList.forEach((item, index) => {
            if (item === child[0]) {
              this.inputForm.costManagementDTOList.splice(index, 1, child[1])
            }
          })
        }
      },
      addCostManagementRow () {
        this.inputForm.costManagementDTOList.push({
          listNo: '',
          listName: '',
          unit: '',
          quantity: '',
          budgetUnitPrice: '',
          budgetTotalPrice: '',
          auditUnitPrice: '',
          auditTotalPrice: '',
          remark: ''
        })
      },
      delCostManagementRow (child) {
        this.inputForm.costManagementDTOList.forEach((item, index) => {
          if (item === child && item.id === '') {
            this.inputForm.costManagementDTOList.splice(index, 1)
          } else if (item === child) {
            item.delFlag = '1'
            this.inputForm.costManagementDTOList.splice(index, 1, item)
          }
        })
      },
      saveCostOtherRow (child) {
        if (child[0] === '') {
          this.inputForm.costOtherDTOList.push(child[1])
        } else {
          this.inputForm.costOtherDTOList.forEach((item, index) => {
            if (item === child[0]) {
              this.inputForm.costOtherDTOList.splice(index, 1, child[1])
            }
          })
        }
      },
      addCostOtherRow () {
        this.inputForm.costOtherDTOList.push({
          listNo: '',
          listName: '',
          unit: '',
          quantity: '',
          budgetUnitPrice: '',
          budgetTotalPrice: '',
          auditUnitPrice: '',
          auditTotalPrice: '',
          remark: ''
        })
      },
      delCostOtherRow (child) {
        this.inputForm.costOtherDTOList.forEach((item, index) => {
          if (item === child && item.id === '') {
            this.inputForm.costOtherDTOList.splice(index, 1)
          } else if (item === child) {
            item.delFlag = '1'
            this.inputForm.costOtherDTOList.splice(index, 1, item)
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            projectCostService.save(this.inputForm).then((data) => {
              this.visible = false
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>

  
